package com.itany.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class TestMvcConfig implements WebMvcConfigurer {
    @Autowired
    private CheckLoginConfig checkLoginConfig;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        1.定义一个convert转换消息对象--字符串解析器
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
//        2.将convert添加到converters中并设置优先级
        converters.add(0,converter);
//        1.定义一个convert转换消息对象--json解析器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2.1添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.DisableCircularReferenceDetect);
        //2.2处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        //3.在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4.将convert添加到converters中并设置优先级
        converters.add(1,fastConverter);
    }

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/showlist").setViewName("userlist");
        registry.addViewController("/showindex").setViewName("index");
        registry.addViewController("/showCommerceTypeList").setViewName("commerce_typelist");
        registry.addViewController("/showCompanyExamineList").setViewName("commpany_examinelist");
        registry.addViewController("/showCompanyMap").setViewName("commpanymap");
        registry.addViewController("/showLifeTypeList").setViewName("life_typelist");
        registry.addViewController("/showLogin").setViewName("login");
        registry.addViewController("/showManager").setViewName("manager");
        registry.addViewController("/showMessageList").setViewName("messagelist");
        registry.addViewController("/showNotice").setViewName("notice");
        registry.addViewController("/showPermission").setViewName("permission");
        registry.addViewController("/showRole").setViewName("role");
        registry.addViewController("/showServerCommerce").setViewName("server_commerce");
        registry.addViewController("/showServerExamineList").setViewName("server_examinelist");
        registry.addViewController("/showServerLife").setViewName("server_life");
        registry.addViewController("/showServerList").setViewName("serverlist");
        registry.addViewController("/showSubAccount").setViewName("subaccount");
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(checkLoginConfig)
                .addPathPatterns("/showindex", "/showlist", "/showCommerceTypeList",
                        "/showCompanyExamineList", "/showCompanyMap",
                        "/showLifeTypeList", "/showManager",
                        "/showMessageList", "/showNotice", "/showPermission",
                        "/showRole", "/showServerCommerce", "/showServerExamineList",
                        "/showServerLife", "/showServerList", "/showSubAccount");
    }

}
